package person.bangbang.im.Androidgin;


import person.bangbang.im.Androidgin.Util.Log;
import person.bangbang.im.Androidgin.UI.ContactList;
import person.bangbang.im.Androidgin.UI.Login;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
/*
 * the Entry activity.
 * dispatch to appropriate activity.
 */
public class AndroidginActivity extends Activity {
	private Context mCon = null;
	
	private ServiceConnection mConn = null;
	private AndroidginService mService = null;
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		boolean autoLogin = PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
							.getBoolean(AndroidginPref.KEY_AUTO_LOGIN, false);
		
		if (autoLogin) {

		}
	}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mCon = getApplicationContext();
		mConn = new ServiceConnection(){
			public void onServiceConnected(ComponentName comp, IBinder service) {
				mService = ((AndroidginService.LocalBinder)service).getService();
				init();
//			    dispatchActivity();
			}
			public void onServiceDisconnected(ComponentName comp) {

			}
			
		};
		bindService(new Intent(AndroidginActivity.this, AndroidginService.class), mConn, BIND_AUTO_CREATE);
    }
    private void init() {
        ((Button)findViewById(R.id.go_ahead)).setOnClickListener(
        		new View.OnClickListener() {
        			public void onClick(View v) {
        				dispatchActivity();
        			}
        		});
        dispatchActivity();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
    	if(KeyEvent.KEYCODE_DPAD_CENTER == keyCode) {
    		startActivity(new Intent(mCon, AboutAndroidgin.class));
    	}
    	return super.onKeyDown(keyCode, event);
    }
    private void dispatchActivity() {
    	if (AndroidginService.STATE_NULL == mService.getState()) {
	    	Intent login = new Intent(mCon, Login.class);
	    	startActivityForResult(login, Androidgin.LOGIN_REQUEST);
    	} else if (AndroidginService.STATE_SIGNNED == mService.getState()){
    		showContact();
    	}
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	if (Androidgin.LOGIN_REQUEST == requestCode) {
    		switch (resultCode) {
			case Androidgin.LOGIN_OK: {
				showContact();
				break;
			}
			case Androidgin.LOGIN_ERR: {
				
				break;
			}
			case Androidgin.LOGIN_CANCEL:{
				
				break;
			}
			default:
				
				break;
			}
     	}    	
    }
    private void showContact() {
		Intent contactList = new Intent(mCon, ContactList.class);
		startActivity(contactList);
		
		unbindService(mConn);
		finish();
    }
}